<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%@ include file="/common/operatehistory.jsp"%>
<%@ include file="/common/user.jsp"%>
<%@ include file="/common/visit.jsp"%>
<%
	Integer uid = token.getUid();
	User user = userByUid(uid);
	Integer deptId = user.getDeptId()==null?-1:user.getDeptId();
	Map result = new HashMap();
	Map resultTable = new HashMap();
	List<Map> visits = QBuilder.db("basedb").select("new map(v.id as visit_id, v.isconfirmed as isconfirmed, v.sendTime as send_time, sq.detail as detail, v.reconfirmedTime as reconfirmed_time)").from("Visit as v,SurveyQuestion as sq").where(new Where("v.doctorUid=", uid).and("v.status=", STATUS_ENABLE).and("sq.status=", STATUS_ENABLE).and("v.category=", SURVEY_QUESTION_CATEGORY).and("sq.id=v.surveyId")).query().getResultObj();
	List<Map> answerList = QBuilder.db("basedb").select("new map(sa.visitId as visit_id, sa.surveyId as survey_id)").from("SurveyAnswer as sa").where(new Where("sa.status=", STATUS_ENABLE).and("sa.hospital=", hospital)).query().getResultObj();
	List<String> answers = new ArrayList<>();
	for (Map answer:answerList) {
		answers.add(Tools.getDesc(answer.get("visit_id")) + "_" + Tools.getDesc(answer.get("survey_id")));
	}
	Integer surveyCount = 0;
	Integer completedCount = 0;
	Integer expiredCount = 0;
	Integer visitCompleteCount = 0;
	Date dateNow = new Date();
	for (Map map:visits) {
		Integer visitId = (Integer)map.get("visit_id");
		Integer state = POINT_NO_START;
		// 节点当天内算作有效
		Date sendDate = DateUtil.parseDateTime(Tools.getDesc(map.get("send_time")).replace("08:00:00", "23:59:59"));
		Integer isconfirmed = (Integer)map.remove("isconfirmed");
		String sendDateTime = Tools.getDesc(map.get("send_time"));
		String confirmDateTime = Tools.getDesc(map.get("reconfirmed_time"));
		if (dateNow.after(sendDate) && isconfirmed != 2)
			state = POINT_EXPIRE;
		// 略过的节点不算待完成
		if (sendDateTime.equals(confirmDateTime)) {
			state = POINT_COMPLETE;
		}
		List<Map> sourceList = JSONUtil.fromJson(Tools.getDesc(map.get("detail")), new TypeReference<List<Map>>(){});
		for (Map source:sourceList) {
			String type = Tools.getDesc(source.get("link_type")).replace(".0", "");
			String linkId = Tools.getDesc(source.get("link_id")).replace(".0", "");
			if (type.equals(LINK_SURVEY_TYPE)) {
				surveyCount++;
				if (state.equals(POINT_EXPIRE))
					expiredCount++;
				if (state.equals(POINT_COMPLETE))
					visitCompleteCount++;
				if (answers.contains(visitId + "_" + linkId))
					completedCount++;
			}
		}
	}
	resultTable.put("to_complete", surveyCount - expiredCount - completedCount - visitCompleteCount);
	resultTable.put("completed", completedCount);
	resultTable.put("expired", expiredCount);
	Map resultPatient = new HashMap();
	List patients =  QBuilder.db("basedb").select("count(*)").from("OperateHistory as o").where(new Where("o.status = ", STATUS_ENABLE).and("o.category=", SECTION_DOCTOR).and("o.oid=", uid).and("operate=", ATTENTION)).query().getResultObj();
	resultPatient.put("total", patients.get(0));
	List visitPatients =  QBuilder.db("basedb").select("count(distinct v.patientUid)").from("Visit as v,OperateHistory as o").where(new Where("v.status = ", STATUS_ENABLE).and("v.hospital=", hospital).and("v.doctorUid=", uid).and("o.status = ", STATUS_ENABLE).and("o.category=", SECTION_DOCTOR).and("o.oid=", uid).and("operate=", ATTENTION).and("o.uid=v.patientUid")).query().getResultObj();
	resultPatient.put("visiting", visitPatients.get(0));
	result.put("table", resultTable);
	result.put("patient", resultPatient);
	List<Integer> list = QBuilder.db("basedb").queryHql("select vc.id from visit_hospital as vh, visit_category as vc where vh.hospital=" + deptId + " and vc.parent_id=vh.category");
	String index = "-1";
	for (int i = 0; i < list.size(); i++) {
		index += "," + list.get(i);
	}
	List<Integer> hot =  QBuilder.db("basedb").queryHql("select vt.oid from visit_template as vt, visit_link as vl where vl.case_id=vt.oid and vl.cate_id in(" + index + ") and vt.category=1 group by vt.oid order by count(vt.oid) desc", 5, 1);
	index = "-1";
	for (int i = 0; i < hot.size(); i++) {
		index += "," + hot.get(i);
	}
	List<Map> templates =  QBuilder.db("basedb").select("new map(id as template_id, template_name as title, template_type as detail)").from("visit_template").where(new Where("status = ", STATUS_ENABLE).and("category=0").and("id in(" + index + ")")).query().getResultObj();
	List<Integer> myTemplates =  QBuilder.db("basedb").select("oid").from("visit_template").where(new Where("status = ", STATUS_ENABLE).and("category=", 1).and("uid=", uid)).query().getResultObj();
	for (Map map:templates) {
		map.put("added", 0);
		Integer id = (Integer)map.get("template_id");
		if (myTemplates.contains(id))
			map.put("added", 1);
	}
	result.put("hot", templates);
	Map resultTemplate = new HashMap();
	resultTemplate.put("total", myTemplates.size());
	List<Integer> usedList =  QBuilder.db("basedb").select("distinct v.score").from("Visit as v,visit_template as vt").where(new Where("v.status = ", STATUS_ENABLE).and("v.category=", 3).and("v.doctorUid=", uid).and("v.hospital=", hospital).and("vt.status=", STATUS_ENABLE).and("vt.id=v.score")).query().getResultObj();
	resultTemplate.put("used", usedList.size());
	result.put("template", resultTemplate);
	msg.put("data", result);
	Tools.encryptPrint(out, msg);
%>
